package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemShortcomingDao;
import com.example.child_protection.dto.info.SystemShortcomingDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemShortcomingRequestDTO;
import com.example.child_protection.dto.response.SystemShortcomingListResponseDTO;
import com.example.child_protection.dto.response.SystemShortcomingResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemShortcomingModel;
import com.example.child_protection.service.SystemShortcomingQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 科室查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemShortcomingQueryServiceImpl extends MyBaseService implements SystemShortcomingQueryService {

    @Autowired
    public SystemShortcomingDao systemShortcomingDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemShortcomingResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_SHORTCOMING_FIND_ID, requestDTO, SystemShortcomingResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemShortcomingResponseDTO>() {
            @Override
            protected SystemShortcomingResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemShortcomingResponseDTO responseDTO = new SystemShortcomingResponseDTO();
                SystemShortcomingModel systemShortcomingModel = systemShortcomingDao.findId(requestDTO.getId());
                SystemShortcomingDTO systemShortcoming = new SystemShortcomingDTO();
                BeanUtils.copyProperties(systemShortcomingModel, systemShortcoming);
                responseDTO.setInfo(systemShortcoming);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemShortcomingListResponseDTO findList(SystemShortcomingRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_SHORTCOMING_FIND_LIST, requestDTO, SystemShortcomingListResponseDTO.class, new MyServiceExecutor<SystemShortcomingRequestDTO, SystemShortcomingListResponseDTO>() {
            @Override
            protected SystemShortcomingListResponseDTO myprocess(SystemShortcomingRequestDTO requestDTO) throws Exception {
                SystemShortcomingListResponseDTO responseDTO = new SystemShortcomingListResponseDTO();
                SystemShortcomingModel req = new SystemShortcomingModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemShortcomingModel> lists = systemShortcomingDao.findPage(req);
                List<SystemShortcomingDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemShortcomingDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
